package com.yanyu.space.sys.controller;

import com.yanyu.space.common.core.bean.web.R;
import com.yanyu.space.sys.bean.po.Menu;
import com.yanyu.space.sys.bean.vo.common.TreeSelect;
import com.yanyu.space.sys.bean.vo.menu.RoleMenuTreeselectVo;
import com.yanyu.space.sys.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/menu")
@Api(tags = "系统菜单")
@AllArgsConstructor
public class MenuController {

    private IMenuService menuService;

    @GetMapping("/list")
    @ApiOperation("查询菜单列表")
    public R<List<Menu>> listMenu(Menu menu) {
        return R.ok(menuService.list(menu));
    }

    @GetMapping("/treeselect")
    @ApiOperation("树形列表")
    public R<List<TreeSelect>> treeselect() {
        return R.ok(menuService.treeselect());
    }

    @GetMapping("/roleMenuTreeselect/{roleId}")
    @ApiOperation("根据角色ID查询菜单下拉树结构")
    public R<RoleMenuTreeselectVo> roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        return R.ok(menuService.roleMenuTreeselect(roleId));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜单信息")
    public R<Menu> getById(@PathVariable("id") Long id) {
        return R.ok(menuService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增菜单")
    public R addMenu(@RequestBody Menu menu) {
        menuService.save(menu);
        return R.ok();
    }

    @PutMapping
    @ApiOperation("修改菜单")
    public R updateMenu(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除菜单")
    public R delete(@PathVariable("id") Long id) {
        menuService.delete(id);
        return R.ok();
    }

}
